﻿using BCGPSMongoData;
using System;
using System.Collections.Generic;
using System.Text;
using AppSocketModel;
using BCGPSCommon;

namespace AppSocketService.DeviceRunStatusService
{
    public class DeviceRunStatusService : IDeviceRunStatusService
    {
        private readonly MongoDBCommand<DeviceRunStatus> mongoDBCommand = new MongoDBCommand<DeviceRunStatus>("DeviceRunStatus");
        public DeviceRunStatusService()
        {

        }


        #region 获取全部
        public PageableList<DeviceRunStatus> GetList(string imei,DateTime start,DateTime end,bool desc,int index,int size)
        {
            index = index > 0 ? index - 1 : 0;
            if (end.Hour == 0 && end.Minute == 0 && end.Second == 0)
                end = end.AddDays(1);
            start = MongoDBDateTime.GetUtc(start);
            end = MongoDBDateTime.GetUtc(end);
            if (string.IsNullOrWhiteSpace(imei))
                return mongoDBCommand.SelectDocumentsAsync(s => s.ReceiveDateTime >= start && s.ReceiveDateTime < end, s => s.DataPacketDateTime, desc, index * size, size).GetAwaiter().GetResult();
            else
            {
                var pg = mongoDBCommand.SelectDocumentsAsync(s => s.IMEI == imei && s.ReceiveDateTime >= start && s.ReceiveDateTime < end, s => s.DataPacketDateTime, desc, index * size, size).GetAwaiter().GetResult();
                DeviceRunStatus temp=null;
                foreach (var item in pg.Items)
                {
                    if(temp != null)
                    {
                        item.RealSpan = DateTimeToChineseStr.GetTimeStr((long)(item.ReceiveDateTime - temp.ReceiveDateTime).TotalSeconds);
                    }
                    temp = item;
                }
                return pg;
            }
        }
        #endregion
    }
}
